//
//  VideoViewController.m
//  ErWa
//
//  Created by 李雪 on 16/3/9.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoCell.h"
#import "VideoModel.h"
#import <UIImageView+WebCache.h>
#import "AFNetworking.h"
#import "VIdeoDetailViewController.h"
#import "NewNestHeader.h"
@interface VideoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong) UITableView *tableView;  //tableView视图
@property(nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong)NSString *str;
@property (nonatomic, strong) VideoCell *cell;
@end

@implementation VideoViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationItem.title = @"视频";//标题
    [self createSubviews];//创建视图
    [self dataHandler];//数据
}

#pragma mark - ///创建视图
-(void)createSubviews
{
    //创建Tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //注册重用池
    [_tableView registerClass:[VideoCell class] forCellReuseIdentifier:@"cell"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //取消点击cell变灰
    [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//     判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        
        
        _cell.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
        self.view.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
        
    } else {
        _cell.backgroundColor = [UIColor clearColor];
        
    }
    
    
    VideoModel *video = _dataArr[indexPath.row];
    _cell.title.text = video.title;
    [_cell.imgView sd_setImageWithURL:[NSURL URLWithString:video.image]placeholderImage:[UIImage imageNamed:@"heng"]];
    _cell.content.text = video.content;
    _cell.guest.text = [NSString stringWithFormat:@"讲师: %@", video.sjs_guest];
    _cell.length.text = [NSString stringWithFormat:@"时长:  %@min", video.vlength];
   [_cell.playBtn addTarget:self action:@selector(beginPlay:) forControlEvents:UIControlEventTouchUpInside];
    _str = video.url;
    return _cell;
}
- (void)beginPlay:(UIButton *)btn{
    VIdeoDetailViewController *video = [[VIdeoDetailViewController alloc]init];
    video.urlStr = _str;
    [self.navigationController pushViewController:video animated:YES];
}

- (void)dataHandler
{
    NSString *url = @"http://mobileapi.to8to.com/index.php";
    NSString *body = @"action=learnList&appid=15&appostype=2&appversion=3.7.0&channel=appstore&idfa=833640DA-F880-49A8-A645-F51B15F05413&model=video&page=1&paging=1&systemversion=9.2&t8t_device_id=01101292-F73D-493C-A526-C6EA42E11C1B&to8to_token=5947402_e0a4f6dc9a59ebefc0587b2d8a7f7338&uid=5947402&version=2.5";
    [AFNetworking POSTWithURL:url body:body completion:^(id result) {
        NSArray *arr = result[@"data"];
        self.dataArr = [VideoModel modelHanderWithArray:arr];
        [_tableView reloadData];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 420;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
